<!DOCTYPE html>
<script src='../../../resources/testharness.js'></script>
<script src='../../../resources/testharnessreport.js'></script>
<style type="text/css">
#box {
    width: 300px;
    height: 100px;
}
</style>
<div id="box" ></div>

<script type="text/javascript">

test(function(t) {
var receivedTap = false;
var input_types = [];
var box = document.getElementById('box');
var targetRect = box.getBoundingClientRect();
var offset = 50;
var x = targetRect.left + offset;
var y = targetRect.top + offset;
const floatPrecision = 0.00001;

function validTapResult(event) {
    receivedTap = true;
    assert_equals(event.target.id, "box");
    assert_approx_equals(event.clientX, x, floatPrecision);
    assert_approx_equals(event.clientY, y, floatPrecision);
    assert_approx_equals(event.screenX, x+window.screenX, floatPrecision);
    assert_approx_equals(event.screenY, y+window.screenY, floatPrecision);
}

function validPointerDownResult(event) {
    input_types.push(event.pointerType);
    assert_equals(event.target.id, "box");
    assert_approx_equals(event.clientX, x, floatPrecision);
    assert_approx_equals(event.clientY, y, floatPrecision);
    assert_approx_equals(event.screenX, x+window.screenX, floatPrecision);
    assert_approx_equals(event.screenY, y+window.screenY, floatPrecision);
}

var testTap = async_test('Send a tap event.');
testTap.step(function () {
    box.addEventListener('click', validTapResult);
    box.addEventListener('pointerdown', validPointerDownResult);
    if (window.chrome && chrome.gpuBenchmarking) {
        var inputs = [chrome.gpuBenchmarking.TOUCH_INPUT, chrome.gpuBenchmarking.MOUSE_INPUT];
        for (var input of inputs) {
            chrome.gpuBenchmarking.tap(x, y, function() {
                assert_true(receivedTap);
                if (input_types.join(' ') == "touch mouse") {
                    testTap.done();
                }
            }, 50, input);
        }
    }
});

}, 'Test that chrome.gpuBenchmarking.tap always taps on the expected position no matter what the screen scale factor is.');

test(function(t) {
        if (window.chrome && chrome.gpuBenchmarking) {
        var exception;
        try {
            chrome.gpuBenchmarking.tap(-1, -1, t.unreached_func('Listener should not fire'));
        } catch(e) {
          exception = e;
        }
        assert_not_equals(exception, undefined);
    }
}, 'Test that chrome.gpuBenchmarking.tap throws an exception on invalid input values.');

</script>
